package com.ji.li.reactadminproexampleservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ji.li.reactadminproexampleservice.entity.menu.Menu;
import com.ji.li.reactadminproexampleservice.entity.menu.MenuRole;
import com.ji.li.reactadminproexampleservice.entity.menu.MenuUserRole;
import com.ji.li.reactadminproexampleservice.entity.menu.Role;
import com.ji.li.reactadminproexampleservice.entity.auth.AuthRole;
import com.ji.li.reactadminproexampleservice.entity.user.User;
import com.ji.li.reactadminproexampleservice.entity.auth.AuthUserRole;
import com.ji.li.reactadminproexampleservice.service.MapperService;
import com.ji.li.reactadminproexampleservice.service.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MapperServiceImpl implements MapperService {
    private final UserService userService;
    private final AuthRoleService authRoleService;
    private final AuthUserRoleService authUserRoleService;
    private final MenuUserRoleService menuUserRoleService;
    private final RoleService roleService;
    private final MenuRoleService menuRoleService;
    private final MenuService menuService;

    @Autowired
    public MapperServiceImpl(UserService userService, AuthRoleService authRoleService,
                             AuthUserRoleService authUserRoleService, MenuUserRoleService menuUserRoleService,
                             RoleService roleService, MenuRoleService menuRoleService, MenuService menuService) {
        this.userService = userService;
        this.authRoleService = authRoleService;
        this.authUserRoleService = authUserRoleService;
        this.menuUserRoleService = menuUserRoleService;
        this.roleService = roleService;
        this.menuRoleService = menuRoleService;
        this.menuService = menuService;
    }

    @Override
    public List<Object> adminLoginAccount(String userName, String passWord) {
        User user = userService.getOne(
                new QueryWrapper<User>()
                        .select("u_id","header","user_name", "pass_word", "enabled")
                        .lambda()
                        .eq(User::getUserName, userName)
                        .eq(User::getPassWord, passWord));
        ArrayList<Object> res = new ArrayList<>();
        res.add(user != null);
        res.add(user);
        return res;
    }

    @Override
    public List<AuthUserRole> selectUserRoles(Integer uid) {
        return authUserRoleService.list(
                new QueryWrapper<AuthUserRole>()
                        .select("ur_id","user_id","role_id")
                        .lambda()
                        .eq(AuthUserRole::getUserId,uid));
    }

    @Override
    public User setUser(Integer uid) {
        return userService.getOne(
                new QueryWrapper<User>()
                        .select("u_id","user_name","pass_word","enabled")
                        .lambda()
                        .eq(User::getuId,uid));
    }

    @Override
    public AuthRole setRole(Integer rId) {
        return authRoleService.getOne(
                new QueryWrapper<AuthRole>()
                        .select("r_id","name","description")
                        .lambda()
                        .eq(AuthRole::getrId,rId));
    }

    @Override
    public User findUserByName(String userName) {
        return userService.getOne(
                new QueryWrapper<User>()
                        .select("u_id","header","user_name", "pass_word", "enabled")
                        .lambda()
                        .eq(User::getUserName,userName));
    }

    @Override
    public List<MenuUserRole> selectMenuUserRole(Integer uId) {
        return menuUserRoleService.list(
                new QueryWrapper<MenuUserRole>()
                        .lambda()
                        .eq(MenuUserRole::getUserId,uId));
    }

    @Override
    public List<Role> selectRole(List<Integer> roleId) {
        return roleService.listByIds(roleId);
    }

    @Override
    public List<MenuRole> selectMenuRole(Integer roleId) {
        return menuRoleService.list(
                new QueryWrapper<MenuRole>()
                        .lambda()
                        .eq(MenuRole::getRoleId,roleId));
    }

    @Override
    public List<Menu> selectMenus(List<Integer> menuId) {
        return menuService.listByIds(menuId);
    }
}
